package com.example.image;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;

import com.example.image.adapter.AdapterShowSelect;
import com.example.image.util.ImageUtil;

public class ImageShow extends Activity {
	private Button btnZoomIn, btnZoomOut, btnTurnLeft, btnTurnRight;
	private ImageView ivShow;
	private Gallery gSelect;
	private AdapterShowSelect adapterShowSelect;
	private ArrayList<Drawable> list = new ArrayList<Drawable>();
	private Bitmap currentBitmap; // 当前显示的图片
	private int width; // 屏幕宽

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_show);
		Display display = getWindowManager().getDefaultDisplay();
		width = display.getWidth();
		ArrayList<String> imagePaths = getIntent().getStringArrayListExtra(
				"imagepaths");
		int position = getIntent().getIntExtra("position", 0);
		initImageList(imagePaths);
		initView();
		initAdapter();
		initListener();
		gSelect.setSelection(position);
	}

	/**
	 * 获取图片列表
	 * 
	 * @param imagePaths
	 *            图片路径列表
	 */
	private void initImageList(ArrayList<String> imagePaths) {
		for (int i = 0; i < imagePaths.size(); i++) {
			Drawable drawable = ImageUtil.getDrawable(imagePaths.get(i), width);
			list.add(drawable);
		}
	}

	private void initView() {
		btnZoomIn = (Button) findViewById(R.id.btn_show_zoom_in);
		btnZoomOut = (Button) findViewById(R.id.btn_show_zoom_out);
		btnTurnLeft = (Button) findViewById(R.id.btn_show_turn_left);
		btnTurnRight = (Button) findViewById(R.id.btn_show_turn_right);
		ivShow = (ImageView) findViewById(R.id.iv_show_view);
		gSelect = (Gallery) findViewById(R.id.g_show_select);
	}

	private void initAdapter() {
		adapterShowSelect = new AdapterShowSelect(this, list);
		gSelect.setAdapter(adapterShowSelect);
	}

	private void initListener() {
		btnTurnLeft.setOnClickListener(btnListener);
		btnTurnRight.setOnClickListener(btnListener);
		btnZoomIn.setOnClickListener(btnListener);
		btnZoomOut.setOnClickListener(btnListener);
		gSelect.setOnItemSelectedListener(listener);
		ivShow.setOnTouchListener(touchListener);
	}

	private OnClickListener btnListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int position = gSelect.getSelectedItemPosition();
			currentBitmap = adapterShowSelect.getBitmap(position);
			switch (v.getId()) {
			case R.id.btn_show_turn_left:
				turnLeft();
				break;
			case R.id.btn_show_turn_right:
				turnRight();
				break;
			case R.id.btn_show_zoom_in:
				zoomIn();
				break;
			case R.id.btn_show_zoom_out:
				zoomOut();
				break;
			default:
				break;
			}
			adapterShowSelect.setBitmap(position, currentBitmap);
			ivShow.setImageDrawable(new BitmapDrawable(null, currentBitmap));
		}
	};
	private OnItemSelectedListener listener = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			ivShow.setImageDrawable(adapterShowSelect.getItem(position));
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	};

	private static final int NONE = 0;
	private static final int DRAG = 1;
	private static final int ZOOM = 2;

	private int mode = NONE;
	private float oldDist;
	private Matrix matrix = new Matrix();
	private Matrix savedMatrix = new Matrix();
	private PointF start = new PointF();
	private PointF mid = new PointF();
	private OnTouchListener touchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				savedMatrix.set(matrix);
				start.set(event.getX(), event.getY());
				mode = DRAG;
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_POINTER_UP:
				mode = NONE;
				break;
			case MotionEvent.ACTION_POINTER_DOWN:
				oldDist = spacing(event);
				if (oldDist > 10f) {
					savedMatrix.set(matrix);
					midPoint(mid, event);
					mode = ZOOM;
				}
				break;
			case MotionEvent.ACTION_MOVE:
				if (mode == DRAG) {
					matrix.set(savedMatrix);
					matrix.postTranslate(event.getX() - start.x, event.getY()
							- start.y);
				} else if (mode == ZOOM) {
					float newDist = spacing(event);
					if (newDist > 10f) {
						matrix.set(savedMatrix);
						float scale = newDist / oldDist;
						matrix.postScale(scale, scale, mid.x, mid.y);
					}
				}
				break;
			}
			ivShow.setImageMatrix(matrix);
			return true;
		}

		private float spacing(MotionEvent event) {
			float x = event.getX(0) - event.getX(1);
			float y = event.getY(0) - event.getY(1);
			return (float) Math.sqrt(x * x + y * y);
		}

		private void midPoint(PointF point, MotionEvent event) {
			float x = event.getX(0) + event.getX(1);
			float y = event.getY(0) + event.getY(1);
			point.set(x / 2, y / 2);
		}
	};

	/**
	 * 向左旋转
	 */
	protected void turnLeft() {
		currentBitmap = ImageUtil.turn(currentBitmap, -90);
	}

	/**
	 * 向右旋转
	 */
	protected void turnRight() {
		currentBitmap = ImageUtil.turn(currentBitmap, 90);
	}

	/**
	 * 缩小
	 */
	protected void zoomOut() {
		currentBitmap = ImageUtil.zoom(currentBitmap, 0.5f);
	}

	/**
	 * 放大
	 */
	protected void zoomIn() {
		currentBitmap = ImageUtil.zoom(currentBitmap, 2);
	}
}
